﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using QUANLYCHAMCONG.BL.BUS;
using QUANLYCHAMCONG.BL.Entities;
using zkemkeeper;
using Hopthoai;
using QUANLYCHAMCONG.BL;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using QUANLYCHAMCONG.BL.BUS.Import;

namespace QUANLYCHAMCONG.PL.CMS.Maychamcong
{
    public partial class DownLoad_vantay : UserControl
    {
       
        string mamay = "";
        string seri = "";
        string serimahoa = "";
        int flag = 0;
        public CZKEM AxCZKEM1 = new CZKEMClass();
        private bool IsConnect;
        frmHoidap f;
        MAYCHAMCONG mcc;
        STAFF nv;
        STAFFCollection dsnv;
        Template vantay;
        TemplateCololection danhsachvantay;
        MAYCHAMCONGCollection danhsachmaychamcong;
        public delegate void _Close();
        public event _Close Close;
        bool IsResignter;
        public DownLoad_vantay()
        {
            InitializeComponent();
        }
        private void DownLoad_vantay_Load(object sender, EventArgs e)
        {
            ActiveToolbar();
            LoadAllMAY();
            SetLink();
        }
        public Total UcStatusRoom { get; set; }
        public void ReloadStattus()
        {
            Thongke tk = new Thongke();
            this.UcStatusRoom.DtData = BAOCAOBUS.ThongkeTotal(tk);
            this.UcStatusRoom.Settext();
        }
        private void ActiveToolbar()
        {
            this.toolbarMayChamCong1.SetEnableButton(true, false, false, false, false, true, true, true, true);
        }
        void LoadAllMAY()
        {
            try
            {
                DataTable dt = new DataTable();
                dt = MAYCHAMCONGBUS.SelectAll(null);
                this.toolbarMayChamCong1.DataLoaiMay = dt;
                this.toolbarMayChamCong1.DisplayMember = "TENMAY";
                this.toolbarMayChamCong1.ValuesMember = "MAMCC";
                this.toolbarMayChamCong1.AddDataForCMB();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi :" + ex.Message.ToString());
            }
            
        }
        private void toolbarMayChamCong1_ChangeMachine()
        {
            mamay = this.toolbarMayChamCong1.SelectValue;
        }
        private void toolbarMayChamCong1_Download()
        {
            if(!string.IsNullOrEmpty(mamay))
            {
               flag = 1;
               this.toolbarMayChamCong1.SetEnableButton(false, false, false, false, true, false, false, false, true);
               if (mamay=="-1")
               {
                   DownloadAll();
               } 
               else
               {
                   DownloadOne();
               }
            }
        }
        private bool Connection(ConnectType type, bool isdomain, MAYCHAMCONG mcc)
        {
            bool IsComplete = false;
            try
            {
                if (type == ConnectType.COM)
                {
                    /// doc toc do may
                    DataTable tocdo = new DataTable();
                    TOCDO td = new TOCDO();
                    td.MATD = mcc.MATD;
                    tocdo = TOCDOBUS.SelectPK(td);
                    int tocdocom = 0;
                    foreach (DataRow rowtocdo in tocdo.Rows)
                    {
                        tocdocom = int.Parse(rowtocdo["TENTOCDO"].ToString());
                        break;
                    }
                    this.IsConnect = AxCZKEM1.Connect_Com(mcc.COMPORT, mcc.IDMAY, tocdocom);
                }
                else if (type == ConnectType.TCP)
                {
                    if (mcc.ISDOMAIN)
                    {
                        this.IsConnect = AxCZKEM1.Connect_Net(mcc.DOMAIN, int.Parse(mcc.PORT));
                    }
                    else
                    {
                        this.IsConnect = AxCZKEM1.Connect_Net(mcc.IP, int.Parse(mcc.PORT));
                    }
                }
                else if (type == ConnectType.USB)
                {
                    this.IsConnect = AxCZKEM1.Connect_USB(mcc.IDMAY);
                }
                IsComplete = true;
            }
            catch (Exception ex)
            {
                IsComplete = false;
            }
            return IsComplete;
        }
        void CreateConection(MAYCHAMCONG mcc)
        {
            bool IsConnectSuss = false;
            if (mcc.MALKN == "COM")
            {
                IsConnectSuss = Connection(ConnectType.COM, mcc.ISDOMAIN, mcc);
            }
            else if (mcc.MALKN == "TCP")
            {
                IsConnectSuss = Connection(ConnectType.TCP, mcc.ISDOMAIN, mcc);
            }
            else if (mcc.MALKN == "USB")
            {
                IsConnectSuss = Connection(ConnectType.USB, mcc.ISDOMAIN, mcc);
            }
            if (!IsConnectSuss)
            {
                CreateConection(mcc);
            }
        }
        bool CheckSeriAl(string serial)
        {
            mahoa.mahoa mh = new mahoa.mahoa();
            serimahoa = mh.mahoadulieu(seri, false, "thanhthuy");
            if (!Common.IsExists(Application.StartupPath + @"\" + serimahoa + ".GDT"))
            {
                ActiveToolbar();
                f = new frmHoidap("Máy không đúng hoặc chưa được đăng ký \n vui lòng kiểm tra lại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                f.ShowDialog();
                return false;
            }
            else
            {
                return true;
            }
        }
        private void DownloadOne()
        {
            try
            {
                
                if (!String.IsNullOrEmpty(mamay))
                {
                    if (mamay.Trim() == "-1")
                    {
                        f = new frmHoidap("Bạn vui lòng chọn máy cụ thể!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.canhbao);
                        f.ShowDialog();
                    }
                    else
                    {
                        danhsachvantay = new TemplateCololection();
                        //thuc hien download 
                        DataTable dt = new DataTable();
                        mcc = new MAYCHAMCONG();
                        mcc.MAMCC = mamay;
                        dt = MAYCHAMCONGBUS.SelectPK(mcc);
                        if (dt.Rows.Count != 0)
                        {
                            //co kha nang gay loi o day
                            mcc = new MAYCHAMCONG();
                            DataTableToMaychamcong(ref mcc, dt);
                            //---------------------------------------------------------------------------------------------------------------
                            frmwaiting wait = new frmwaiting();
                            wait.StartPosition = FormStartPosition.CenterScreen;
                            wait.lbgoi.Text = "Đang gọi máy " + mcc.IDMAY.ToString() + ".....";
                            wait.Show();
                            Application.DoEvents();
                            //goi ham tao ket noi
                            CreateConection(mcc);
                            //--------------------------------------------------------------------------------------------------
                            if (IsConnect)
                            {
                                AxCZKEM1.GetSerialNumber(mcc.IDMAY, out seri);
                                if (!CheckSeriAl(seri))
                                {
                                    wait.Close();
                                    AxCZKEM1.Disconnect();
                                }
                                else
                                {
                                    IsResignter = true;
                                }
                                if (IsResignter)
                                {

                                    int dwMachineNumber = mcc.IDMAY;
                                    string sdwEnrollNumber = "";
                                    string sName = "";
                                    string sPassword = "";
                                    int iPrivilege = 0;
                                    bool bEnabled = false;
                                    int dwValues = 0;
                                    int idwFingerIndex;
                                    string sTmpData = "";
                                    int iTmpLength = 0;
                                    int iFlag = 0;
                                    int count = 0;
                                    wait.lbgoi.Text = "Reading ...";
                                    wait.lbphantram.Visible = true;
                                    wait.tocdo.Visible = true;
                                    wait.lbphantram.Text = "0 %";
                                    Application.DoEvents();
                                    //VIET LAI HAM NAY 
                                    int num = 0;
                                    AxCZKEM1.EnableDevice(dwMachineNumber, false);
                                    danhsachvantay = new TemplateCololection();
                                    AxCZKEM1.GetDeviceStatus(dwMachineNumber, 3, ref dwValues);
                                    AxCZKEM1.GetDeviceStatus(dwMachineNumber, 2, ref count);
                                    wait.tocdo.Maximum = count;
                                    Application.DoEvents();
                                    if (dwValues == 0)
                                    {
                                        wait.Close();
                                        ActiveToolbar();
                                        f = new frmHoidap("Không có dữ liệu về nhân viên vân tay!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                        f.ShowDialog();
                                        AxCZKEM1.Disconnect();
                                        return;
                                    }
                                    else
                                    {
                                        AxCZKEM1.ReadAllUserID(dwMachineNumber);//read all the user information to the memory
                                        AxCZKEM1.ReadAllTemplate(dwMachineNumber);//read all the users' fingerprint templates to the memory
                                        while (AxCZKEM1.SSR_GetAllUserInfo(dwMachineNumber, out sdwEnrollNumber, out sName, out sPassword, out iPrivilege, out bEnabled))//get all the users' information from the memory
                                        {
                                            for (idwFingerIndex = 0; idwFingerIndex < 10; idwFingerIndex++)
                                            {
                                                if (AxCZKEM1.GetUserTmpExStr(dwMachineNumber, sdwEnrollNumber, idwFingerIndex, out iFlag, out sTmpData, out iTmpLength))//get the corresponding templates string and length from the memory
                                                {
                                                    vantay = new Template();
                                                    vantay.FingerID = idwFingerIndex;
                                                    vantay.TempLength = iTmpLength;
                                                    vantay.MACCM = sdwEnrollNumber;
                                                    vantay.MAVT = null;
                                                    vantay.FingerTemplate = sTmpData;
                                                    danhsachvantay.Add(vantay);
                                                }
                                            }
                                            wait.tocdo.Value = num++;
                                            wait.lbphantram.Text = wait.tocdo.Value.ToString() + " % ";
                                            Application.DoEvents();
                                        }
                                    }
                                   

                                //---------------------------------------------------------------------------
                                
                                // ---------------------------------------------------------------------------
   
                                        //sau khi doc song 
                                        wait.Close();
                                        AxCZKEM1.EnableDevice(dwMachineNumber, true);
                                        AxCZKEM1.Disconnect();
                                        this.toolbarMayChamCong1.SetEnableButton(false, false, false, false, true, false, false, false, true);
                                        f = new frmHoidap("Đọc song dữ liệu vân tay !", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                                        f.ShowDialog();
                                        //thuc hien load du lieu
                                        DataTable ds = new DataTable();
                                        CreateTableTemplate(ref ds);
                                        ConvertTempleteToTable(ref ds, mcc.IDMAY.ToString());
                                        this.dsvantay.DataSource = ds;
                                }
                                else
                                {
                                    wait.Close();
                                    ActiveToolbar();
                                    f = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + " vui lòng kiểm tra lại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                    f.ShowDialog();
                                }
                            }

                        }
                        else
                        {
                            
                            f = new frmHoidap("Không có dữ liệu vui lòng kiểm tra lại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                            f.ShowDialog();
                        }
                    }
                   
                }
            }
            catch (Exception ex)
            {

                f = new frmHoidap("Có lỗi : " + ex.Message.ToString(), frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                f.ShowDialog();
            }

        }
        private void DownloadAll()
        { 
            if(!String.IsNullOrEmpty(mamay))
            {
                if (mamay=="-1")
                {
                    try
                    {
                        int SumMachine = Tongsomay();
                        danhsachvantay = new TemplateCololection();
                        DataTable dt = new DataTable();
                        dt = MAYCHAMCONGBUS.SelectAll(null);
                        ConvertData(dt);
                        //doc tu danh sach may va download 
                        foreach (MAYCHAMCONG macc in danhsachmaychamcong)
                        {
                            mcc = new MAYCHAMCONG();
                            mcc = macc;
                            DataTableToMaychamcong(ref mcc, dt);
                            frmwaiting wait = new frmwaiting();
                            wait.StartPosition = FormStartPosition.CenterScreen;
                            wait.lbgoi.Text = "Đang gọi máy " + mcc.IDMAY.ToString() + ".....";
                            // wait.ShowDialog();
                            wait.Show();
                            Application.DoEvents();
                            CreateConection(mcc);
                            if (IsConnect)
                            {

                                AxCZKEM1.GetSerialNumber(mcc.IDMAY, out seri);
                                if (!CheckSeriAl(seri))
                                {
                                    wait.Close();
                                    continue;
                                }
                                else
                                {
                                    IsResignter = true;
                                }
                                if (IsResignter)
                                {
                                    //lay toan bo danh sach nhan vien ve 
                                    DataTable bangnv = new DataTable();
                                    bangnv = STAFFBUS.SelectAll();
                                    if (bangnv.Rows.Count != 0)
                                    {
                                        // thuc hien bien doi nhan viens
                                        dsnv = new STAFFCollection();
                                        ConvertTableToNhanVien(ref dsnv, bangnv);
                                        // thuc hien download templete
                                        int count = dsnv.Count;
                                        int num = 0;
                                        int iMachineNumber = mcc.IDMAY;
                                        string sdwEnrollNumber = "";
                                        string sName = "";
                                        string sPassword = "";
                                        int iPrivilege = 0;
                                        bool bEnabled = false;
                                        int idwFingerIndex;
                                        string sTmpData = "";
                                        int iTmpLength = 0;
                                        int iFlag = 0;
                                        //-----------------------------------------------------
                                        wait.lbgoi.Text = "Reading ...";
                                        wait.lbphantram.Visible = true;
                                        wait.tocdo.Visible = true;
                                        wait.lbphantram.Text = "0 %";
                                        Application.DoEvents();
                                        danhsachvantay = new TemplateCololection();
                                        //---------------------------------------------------------------------------
                                        wait.tocdo.Maximum = count;
                                        AxCZKEM1.ReadAllUserID(iMachineNumber);//read all the user information to the memory
                                        AxCZKEM1.ReadAllTemplate(iMachineNumber);//read all the users' fingerprint templates to the memory
                                        while (AxCZKEM1.SSR_GetAllUserInfo(iMachineNumber, out sdwEnrollNumber, out sName, out sPassword, out iPrivilege, out bEnabled))//get all the users' information from the memory
                                        {
                                            for (idwFingerIndex = 0; idwFingerIndex < 10; idwFingerIndex++)
                                            {
                                                if (AxCZKEM1.GetUserTmpExStr(iMachineNumber, sdwEnrollNumber, idwFingerIndex, out iFlag, out sTmpData, out iTmpLength))//get the corresponding templates string and length from the memory
                                                {
                                                    vantay = new Template();
                                                    vantay.FingerID = idwFingerIndex;
                                                    vantay.TempLength = iTmpLength;
                                                    vantay.MACCM = sdwEnrollNumber;
                                                    vantay.MAVT = null;
                                                    vantay.FingerTemplate = sTmpData;
                                                    danhsachvantay.Add(vantay);
                                                }
                                            }
                                            wait.tocdo.Value = num++;
                                            wait.lbphantram.Text = wait.tocdo.Value.ToString() + " % ";
                                            Application.DoEvents();
                                        }
                                        wait.Close();
                                        AxCZKEM1.EnableDevice(iMachineNumber, true);
                                        AxCZKEM1.Disconnect();
                                        this.toolbarMayChamCong1.SetEnableButton(false, false, false, false, true, false, false, false, true);
                                        f = new frmHoidap("Đọc song dữ liệu vân tay !", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                                        f.ShowDialog();
                                    }
                                    else
                                    {
                                        wait.Close();
                                        wait.Dispose();
                                        ActiveToolbar();
                                        f = new frmHoidap("Không có dữ liệu về nhân viên " + mcc.IDMAY.ToString() + " vui lòng kiểm tra lại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                        f.ShowDialog();
                                    }
                                }
                                else
                                {
                                    wait.Close();
                                    wait.Dispose();
                                    ActiveToolbar();
                                    f = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + " vui lòng kiểm tra lại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                    f.ShowDialog();
                                }

                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        f = new frmHoidap("Có lỗi : " + ex.Message.ToString(), frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                        f.ShowDialog();
                    }
                    finally
                    {
                        DataTable ds = new DataTable();
                        CreateTableTemplate(ref ds);
                        ConvertTempleteToTable( ref ds, mcc.IDMAY.ToString());
                        this.dsvantay.DataSource = ds;
                    }
                } 
                else
                {
                    DownloadOne();
                }
            }
        }
        private int Tongsomay()
        {
            int somgay = 0;
            DataTable dt = new DataTable();
            dt = MAYCHAMCONGBUS.SumMachineZK();
            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    somgay = int.Parse(dr["tongsomayzk"].ToString());
                    break;
                }
            }
            else
            {
                somgay = 0;
            }
            return somgay;
        }
        private void ConvertData(DataTable dt)
        {
            try
            {
                danhsachmaychamcong = new MAYCHAMCONGCollection();
                foreach (DataRow row in dt.Rows)
                {
                    mcc = new MAYCHAMCONG();
                    mcc.COMPORT = int.Parse(row["COMPORT"].ToString());
                    mcc.DOMAIN = row["DOMAIN"].ToString();
                    mcc.FIMWARE = row["FIMWARE"].ToString();
                    mcc.IDMAY = int.Parse(row["IDMAY"].ToString());
                    mcc.IP = row["IP"].ToString();
                    if (row["ISDOMAIN"].ToString() == "USE")
                        mcc.ISDOMAIN = true;
                    else
                        mcc.ISDOMAIN = false;
                    mcc.MALKN = row["MALKN"].ToString();
                    mcc.MALM = row["MALM"].ToString();
                    mcc.MAMCC = row["MAMCC"].ToString();
                    mcc.MATD = row["MATD"].ToString();
                    mcc.PASSWORD = row["PASSWORD"].ToString();
                    mcc.PORT = row["PORT"].ToString();
                    mcc.SeriNumber = row["SeriNumber"].ToString();
                    mcc.SODANGKI = row["SODANGKI"].ToString();
                    mcc.TENMAY = row["TENMAY"].ToString();
                    mcc.TONGSOMATMA = int.Parse(row["TONGSOMATMA"].ToString());
                    mcc.TONGSONHANVIEN = int.Parse(row["TONGSONHANVIEN"].ToString());
                    mcc.TONGSOVANTAY = int.Parse(row["TONGSOVANTAY"].ToString());
                    danhsachmaychamcong.Add(mcc);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi :" + ex.Message.ToString());
            }
        }
        private void DataTableToMaychamcong(ref MAYCHAMCONG mcc, DataTable dt)
        {

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    mcc = new MAYCHAMCONG();
                    mcc.COMPORT = int.Parse(row["COMPORT"].ToString());
                    mcc.DOMAIN = row["DOMAIN"].ToString();
                    mcc.FIMWARE = row["FIMWARE"].ToString();
                    mcc.IDMAY = int.Parse(row["IDMAY"].ToString());
                    mcc.IP = row["IP"].ToString();
                    if (row["ISDOMAIN"].ToString() == "USE")
                        mcc.ISDOMAIN = true;
                    else
                        mcc.ISDOMAIN = false;
                    mcc.MALKN = row["MALKN"].ToString();
                    mcc.MALM = row["MALM"].ToString();
                    mcc.MAMCC = row["MAMCC"].ToString();
                    mcc.MATD = row["MATD"].ToString();
                    mcc.PASSWORD = row["PASSWORD"].ToString();
                    mcc.PORT = row["PORT"].ToString();
                    mcc.SeriNumber = row["SeriNumber"].ToString();
                    mcc.SODANGKI = row["SODANGKI"].ToString();
                    mcc.TENMAY = row["TENMAY"].ToString();
                    mcc.TONGSOMATMA = int.Parse(row["TONGSOMATMA"].ToString());
                    mcc.TONGSONHANVIEN = int.Parse(row["TONGSONHANVIEN"].ToString());
                    mcc.TONGSOVANTAY = int.Parse(row["TONGSOVANTAY"].ToString());
                }
            }
            else
            {
                f = new frmHoidap("Không có dữ liệu Máy chấm công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                f.ShowDialog();
            }
        }
        private void ConvertTableToNhanVien(ref STAFFCollection danhsachnhanvien, DataTable dt)
        {
            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    nv = new STAFF();
                    nv.MANV = dr["MANV"].ToString();
                    nv.MACCM = dr["MACCM"].ToString();
                    nv.TENCC = dr["TENCC"].ToString();
                    nv.UserPW = dr["UserPW"].ToString();
                    nv.UserPrivilege = int.Parse(dr["UserPrivilege"].ToString());
                    if ((dr["UserEnabled"].ToString() == "WORK"))
                        nv.UserEnabled = true;
                    else
                        nv.UserEnabled = false;
                    nv.UserCardNo = dr["UserCardNo"].ToString();
                    danhsachnhanvien.Add(nv);
                }
            }
            else
            {
                f = new frmHoidap("Không có dữ liệu nhân viên!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                f.ShowDialog();
            }
        }
        private void CreateTableTemplate(ref DataTable dt)
        {
            try
            {
                DataColumn STT = new DataColumn("STT", typeof(Int32));
                DataColumn MachineNo = new DataColumn("MachineNo", typeof(Int32));
                DataColumn UserEnrollNumber = new DataColumn("UserEnrollNumber", typeof(string));
                DataColumn FingerID = new DataColumn("FingerID", typeof(Int32));
                DataColumn TempLength = new DataColumn("TempLength", typeof(Int32));
                DataColumn FingerTemplate = new DataColumn("FingerTemplate", typeof(string));
                dt.Columns.AddRange(new DataColumn[] { STT, MachineNo, UserEnrollNumber, FingerID, TempLength, FingerTemplate });
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi :"+ex.Message.ToString());
            }
        }
        private void ConvertTempleteToTable(ref DataTable dt,string idmay)
        {
                int i = 1;
                foreach (Template t in danhsachvantay)
                {
                    DataRow dr = dt.NewRow();
                    dr["STT"] = i;
                    dr["MachineNo"] = idmay;
                    dr["UserEnrollNumber"] = t.MACCM;
                    dr["FingerID"] = t.FingerID;
                    dr["TempLength"] = t.TempLength;
                    dr["FingerTemplate"] = t.FingerTemplate;
                    dt.Rows.Add(dr);
                    i++;
                }
        }
        private void toolbarMayChamCong1_Save()
        {
            try
            {
                bool IsComplete = true;
                if (flag == 1 && dsvantay.Rows.Count != 0)//download song
                {
                    foreach (Template temp in danhsachvantay)
                    {
                        IsComplete = TemplateBUS.Insert(temp);
                    }
                    if (IsComplete)
                    {
                        f = new frmHoidap("Lưu vân tay thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                        f.ShowDialog();
                        ReloadStattus();
                        ActiveToolbar();
                    }
                    else
                    {
                        f = new frmHoidap("Có lỗi khi lưu vân tay \n hoặc vân tay đã tồn tại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                        f.ShowDialog();
                        ReloadStattus();
                        ActiveToolbar();
                    }
                }
            }
            catch (Exception ex)
            {
                f = new frmHoidap("Lỗi :"+ex.Message.ToString(), frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                f.ShowDialog();
                ReloadStattus();
                ActiveToolbar();
            }
        }
        private void toolbarMayChamCong1_Restart()
        {
            if (!String.IsNullOrEmpty(mamay))
            {
                if (mamay.Trim() == "-1")
                {
                    f = new frmHoidap("Bạn vui lòng chọn máy cụ thể!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.canhbao);
                    f.ShowDialog();
                }
                else
                {
                    DataTable dt = new DataTable();
                    mcc = new MAYCHAMCONG();
                    mcc.MAMCC = mamay;
                    dt = MAYCHAMCONGBUS.SelectPK(mcc);
                    if (dt.Rows.Count != 0)
                    {
                        mcc = new MAYCHAMCONG();
                        DataTableToMaychamcong(ref mcc, dt);
                        frmwaiting wait = new frmwaiting();
                        wait.StartPosition = FormStartPosition.CenterScreen;
                        wait.Show();
                        wait.lbgoi.Text = "Đang gọi máy " + mcc.IDMAY.ToString() + ".....";
                        Application.DoEvents();
                        CreateConection(mcc);
                        if (IsConnect)
                        {
                            AxCZKEM1.GetSerialNumber(mcc.IDMAY, out seri);
                            if (!CheckSeriAl(seri))
                            {
                                wait.Close();
                                AxCZKEM1.Disconnect();
                            }
                            else
                            {
                                IsResignter = true;
                            }
                            if (IsResignter)
                            {
                                if (!AxCZKEM1.RestartDevice(mcc.IDMAY))
                                {
                                    wait.Close();
                                    f = new frmHoidap("Khởi động lại máy " + mcc.IDMAY.ToString() + "thành cộng", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                    f.ShowDialog();
                                    AxCZKEM1.Disconnect();
                                }
                                else
                                {
                                    wait.Close();
                                    f = new frmHoidap(mcc.IDMAY + "Khởi động lại thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                    f.ShowDialog();
                                    AxCZKEM1.Disconnect();
                                }
                            }
                        }
                        if (!IsConnect)
                        {
                            wait.Close();
                            wait.Dispose();
                            ActiveToolbar();
                            f = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + " vui lòng kiểm tra lại", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                            f.ShowDialog();
                            AxCZKEM1.Disconnect();
                        }
                    }
                }
            }
        }
        private void toolbarMayChamCong1_Syssnc()
        {
            int dwYear = DateAndTime.Year(DateAndTime.Now);
            int dwMonth = DateAndTime.Month(DateAndTime.Now);
            int dwDay = DateAndTime.Day(DateAndTime.Now);
            int dwHour = DateAndTime.Hour(DateAndTime.Now);
            int dwMinute = DateAndTime.Minute(DateAndTime.Now);
            int dwSecond = DateAndTime.Second(DateAndTime.Now);
            if (!String.IsNullOrEmpty(mamay))
            {
                if (mamay.Trim() == "-1")
                {
                    f = new frmHoidap("Bạn vui lòng chọn máy cụ thể!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.canhbao);
                    f.ShowDialog();
                }
                else
                {
                    DataTable dt = new DataTable();
                    mcc = new MAYCHAMCONG();
                    mcc.MAMCC = mamay;
                    dt = MAYCHAMCONGBUS.SelectPK(mcc);
                    if (dt.Rows.Count != 0)
                    {
                        mcc = new MAYCHAMCONG();
                        DataTableToMaychamcong(ref mcc, dt);
                        frmwaiting wait = new frmwaiting();
                        wait.StartPosition = FormStartPosition.CenterScreen;
                        wait.Show();
                        wait.lbgoi.Text = "Đang gọi máy " + mcc.IDMAY.ToString() + ".....";
                        Application.DoEvents();
                        #region "Kết nối"
                        if (mcc.MALKN == "COM")
                        {
                            Connection(ConnectType.COM, mcc.ISDOMAIN, mcc);
                        }
                        else if (mcc.MALKN == "TCP")
                        {
                            Connection(ConnectType.TCP, mcc.ISDOMAIN, mcc);
                        }
                        else if (mcc.MALKN == "USB")
                        {
                            Connection(ConnectType.USB, mcc.ISDOMAIN, mcc);
                        }
                        #endregion
                        if (IsConnect)
                        {
                            AxCZKEM1.GetSerialNumber(mcc.IDMAY, out seri);
                            if (!CheckSeriAl(seri))
                            {
                                wait.Close();
                                AxCZKEM1.Disconnect();
                            }
                            else
                            {
                                IsResignter = true;
                            }
                            if (IsResignter)
                            {
                                if (!AxCZKEM1.SetDeviceTime2(mcc.IDMAY, dwYear, dwMonth, dwDay, dwHour, dwMinute, dwSecond))
                                {
                                    wait.Close();
                                    f = new frmHoidap("Không thể đồng bộ thời gian với " + mcc.IDMAY.ToString() + " vui lòng kiểm tra lại", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                    f.ShowDialog();
                                    AxCZKEM1.Disconnect();
                                }
                                else
                                {
                                    wait.Close();
                                    f = new frmHoidap("Đồng bộ thời gian với " + mcc.IDMAY.ToString() + " thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                                    f.ShowDialog();
                                    AxCZKEM1.Disconnect();
                                }
                            }

                        }
                        if (!IsConnect)
                        {
                            wait.Close();
                            wait.Dispose();
                            ActiveToolbar();
                            f = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + " vui lòng kiểm tra lại", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                            f.ShowDialog();
                        }
                    }
                }
            }
        }
        private void toolbarMayChamCong1_Clock()
        {
            if (!String.IsNullOrEmpty(mamay))
            {
                if (mamay.Trim() == "-1")
                {
                    f = new frmHoidap("Bạn vui lòng chọn máy cụ thể!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.canhbao);
                    f.ShowDialog();
                }
                else
                {
                    try
                    {
                        DataTable dt = new DataTable();
                        mcc = new MAYCHAMCONG();
                        mcc.MAMCC = mamay;
                        dt = MAYCHAMCONGBUS.SelectPK(mcc);
                        if (dt.Rows.Count != 0)
                        {
                            mcc = new MAYCHAMCONG();
                            DataTableToMaychamcong(ref mcc, dt);
                            frmwaiting wait = new frmwaiting();
                            wait.StartPosition = FormStartPosition.CenterScreen;
                            wait.lbgoi.Text = "Đang gọi máy " + mcc.IDMAY.ToString() + ".....";
                            wait.Show();
                            Application.DoEvents();
                            CreateConection(mcc);
                            if (IsConnect)
                            {
                                AxCZKEM1.GetSerialNumber(mcc.IDMAY, out seri);
                                if (!CheckSeriAl(seri))
                                {
                                    wait.Close();
                                    AxCZKEM1.Disconnect();
                                }
                                else
                                {
                                    IsResignter = true;
                                }
                                if (IsResignter)
                                {
                                    int num = 0;
                                    int num2 = 0;
                                    int num3 = 0;
                                    int num4 = 0;
                                    int num5 = 0;
                                    int num6 = 0;
                                    if (!AxCZKEM1.GetDeviceTime(mcc.IDMAY, ref num6, ref num3, ref num, ref num2, ref num4, ref num5))
                                    {
                                        wait.Close();
                                        f = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + " vui lòng kiểm tra lại", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                        f.ShowDialog();
                                    }
                                    else
                                    {
                                        wait.Close();
                                        DateTime dateValue = Conversions.ToDate(Conversions.ToString(num6) + "-" + Conversions.ToString(num3) + "-" + Conversions.ToString(num) + " " + Conversions.ToString(num2) + ":" + Conversions.ToString(num4) + ":" + Conversions.ToString(num5));
                                        string str = Until.LayThu(DateAndTime.Weekday(dateValue, FirstDayOfWeek.Sunday));
                                        f = new frmHoidap(mcc.IDMAY + ":" + str + ", " + Conversions.ToString(dateValue), frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                        f.ShowDialog();
                                        AxCZKEM1.Disconnect();
                                    }
                                }
                            }
                            if (!IsConnect)
                            {
                                wait.Close();
                                ActiveToolbar();
                                f = new frmHoidap("Không thể mở kết nối đến " + mcc.IDMAY.ToString() + " vui lòng kiểm tra lại", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                f.ShowDialog();
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
               
            }
        }
        private void SetLink()
        {
            this.links1.SetValues("Máy", "Download vân tay");
        }
        private void toolbarMayChamCong1_Exit()
        {
            if (this.Close != null)
                this.Close();
        }
    }
}
